<?php


class cartAction extends BaseAction
{
	public function index()
	{		
        $this->require_login();
		$id = intval($_REQUEST['id']);
		$deal_item = M('deal_item')->where(array('id'=>$id))->find();
		if(!$deal_item)
		{
			$this->redirect('/');
		}
		elseif($deal_item['support_count']>=$deal_item['limit_user']&&$deal_item['limit_user']!=0)
		{
            $this->redirect('/deal/show/?id='.$deal_item['deal_id']);
		}
		$deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
		if(!$deal_info)
		{
            $this->redirect('/');
		}
		elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
		{
            $this->redirect('/deal/show/?id='.$deal_item['deal_id']);
		}

        if($deal_info['user_id'] == $this->uid){
            $this->error('您不能购买自己发布的产品');
        }
		
		$deal_item['price_format'] = number_price_format($deal_item['price']);
		$deal_item['delivery_fee_format'] = number_price_format($deal_item['delivery_fee']);
        if($deal_item['is_delivery'] == '1'){
            if($deal_item['delivery_fee'] > 0){
                $deal_item['price_format'] = number_price_format($deal_item['price'] + $deal_item['delivery_fee']);
            }
        }
		$deal_info['percent'] = round($deal_info['support_amount']/$deal_info['limit_price']*100);
		$deal_info['remain_days'] = floor(($deal_info['end_time'] - TIME_NOW)/(24*3600));




		$this->assign("deal_item",$deal_item);
		$this->assign("deal_info",$deal_info);
		
		
		if($deal_info['seo_title']!="")
		$this->assign("seo_title",$deal_info['seo_title']);
		if($deal_info['seo_keyword']!="")
		$this->assign("seo_keyword",$deal_info['seo_keyword']);
		if($deal_info['seo_description']!="")
		$this->assign("seo_description",$deal_info['seo_description']);
		$this->assign("page_title",$deal_info['name']);

		if($deal_item['is_delivery'] == '1')
		{
			$consignee_list = M('user_consignee')->where(array('user_id'=>intval($this->uid)))->select();
            $this->assign("consignee_list",$consignee_list);
		}

		$this->display();
		
	}
	
	public function check()
	{
		$ajax = intval($_REQUEST['ajax']);
        $this->require_login();
		$id = intval($_REQUEST['id']);
		$deal_item = M('deal_item')->where(array('id'=>$id))->find();
		if(!$deal_item)
		{
            $this->error("购买的项目不存在",$ajax);
		}
		elseif($deal_item['support_count']>=$deal_item['limit_user']&&$deal_item['limit_user']!=0)
		{
            $this->redirect("/deal/show",array("id"=>$deal_item['deal_id']),2,"该项目支持数已满");
		}
		$deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
		if(!$deal_info)
		{
            $this->error("购买的项目不存在",$ajax);
		}
		elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
		{
            $this->redirect('/deal/show',array("id"=>$deal_item['deal_id']),2,'该项目数支持已满');
		}
		$consignee_id = 0;
		if($deal_item['is_delivery']==1)
		{
			$consignee_id = intval($_REQUEST['consignee_id']);
			if($consignee_id==0)
			{
				$consignee_list = M('user_consignee')->where(array('user_id'=>$this->uid))->select();
				if($consignee_list)
				{
                    $this->error('请选择收件地址',$ajax);
				}
			}			
		}
		
		if(intval($consignee_id)==0&&$deal_item['is_delivery']==1)
		{
			$this->error('请选择收件地址',$ajax);
		}
		else
		{
			$memo = strim($_REQUEST['memo']);
			//if($memo!=""&&$memo!="在此填写关于回报内容的具体选择或者任何你想告诉项目发起人的话")
            $payment_data = M('payment')->where(array('is_effect'=>'1'))->find();
			if($deal_item['is_delivery']==0){
                $this->redirect('/cart/go_pay',array('id'=>$id,'consignee_id'=>$consignee_id,'credit'=>'0','payment'=>$payment_data['id'],'memo'=>$memo),2,'正在处理');
            }
			else{
                $this->redirect('/cart/go_pay',array("id"=>$id,'consignee_id'=>$consignee_id,'credit'=>'0','payment'=>$payment_data['id'],"did"=>$consignee_id),2,'正在处理');
            }
            exit();
		}
		
		
	}
	
//	public function pay()
//	{
//        $this->require_login();
//		$id = intval($_REQUEST['id']);
//		$deal_item = M('deal_item')->where(array('id'=>$id))->find();
//		if(!$deal_item)
//		{
//			$this->error("购买的项目不存在");
//		}
//		$deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
//		if(!$deal_info)
//		{
//            $this->error("购买的项目不存在");
//		}
//		elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
//		{
//            $this->error("购买的项目已过期");
//		}
//
//		$deal_item['price_format'] = number_price_format($deal_item['price']);
//		$deal_item['delivery_fee_format'] = number_price_format($deal_item['delivery_fee']);
//		$deal_item['total_price'] = $deal_item['price']+$deal_item['delivery_fee'];
//		$deal_item['total_price_format'] = number_price_format($deal_item['total_price']);
//		$deal_info['percent'] = round($deal_info['support_amount']/$deal_info['limit_price']*100);
//		$deal_info['remain_days'] = floor(($deal_info['end_time'] - TIME_NOW)/(24*3600));
//
//		$this->assign("deal_item",$deal_item);
//		$this->assign("deal_info",$deal_info);
//
//
//		if($deal_info['seo_title']!="")
//		$this->assign("seo_title",$deal_info['seo_title']);
//		if($deal_info['seo_keyword']!="")
//		$this->assign("seo_keyword",$deal_info['seo_keyword']);
//		if($deal_info['seo_description']!="")
//		$this->assign("seo_description",$deal_info['seo_description']);
//		$this->assign("page_title",$deal_info['name']);
//
//		$memo = session("cart_memo_".$id);
//		$consignee_id = intval($_REQUEST['did']);
//		$this->assign("memo",$memo);
//		$this->assign("consignee_id",$consignee_id);
//
//		$payment_list = M('payment')->where(array('is_effect'=>'1'))->order('sort asc')->select();
//		$payment_html = "";
//		foreach($payment_list as $k=>$v)
//		{
//			$class_name = $v['class_name']."_payment";
//			require_once SITE_PATH."system/payment/".$class_name.".php";
//			$o = new $class_name;
//			$payment_html .= "<div>".$o->get_display_code()."<div class='blank'></div></div>";
//		}
//		$this->assign("payment_html",$payment_html);
//
//
//		$this->display();
//	}

	public function go_pay()
	{

        $this->require_login();
		$id = intval($_REQUEST['id']);
		$consignee_id = intval($_REQUEST['consignee_id']);
		$credit = doubleval($_REQUEST['credit']);
		
		
		$memo = strim($_REQUEST['memo']);
		$payment_id = intval($_REQUEST['payment']);
        if(empty($payment_id)){
            $this->error('请选择支付方式');
        }
        M('deal_item')->startTrans();
		$deal_item = M('deal_item')->where(array('id'=>$id))->find();
		if(!$deal_item)
		{
            $this->error("购买的项目不存在");
		}
		elseif($deal_item['support_count']>=$deal_item['limit_user']&&$deal_item['limit_user']!=0)
		{
            $this->redirect('/deal/show/',array("id"=>$deal_item['deal_id']),1,'您购买的项目超出人数限制');
		}
		$deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
		if(!$deal_info)
		{
            $this->error("购买的项目不存在");
		}
		elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
		{
            $this->redirect('/deal/show/',array("id"=>$deal_item['deal_id']),1,'购买的项目已过期');
		}
		
		if(intval($consignee_id)==0&&$deal_item['is_delivery']==1)
		{
            $this->error('请选择收件地址');
		}
		
		$order_info['deal_id'] = $deal_info['id'];
		$order_info['deal_item_id'] = $deal_item['id'];
		$order_info['user_id'] = intval($this->uid);
		$order_info['user_name'] = $this->userinfo['user_name'];
		$order_info['total_price'] = $deal_item['price']+$deal_item['delivery_fee'];
		$order_info['delivery_fee'] = $deal_item['delivery_fee'];
		$order_info['deal_price'] = $deal_item['price'];
		$order_info['support_memo'] = $memo;
		$order_info['payment_id'] = $payment_id;
		$order_info['bank_id'] = strim($_REQUEST['bank_id']);
		
		$max_credit= $order_info['total_price']<$this->userinfo['money']?$order_info['total_price']:$this->userinfo['money'];
		$credit = $credit>$max_credit?$max_credit:$credit;
		
		$order_info['credit_pay'] = $credit;
		$order_info['online_pay'] = 0;
		$order_info['deal_name'] = $deal_info['name'];
		$order_info['order_status'] = 0;
		$order_info['create_time']	= TIME_NOW;
		if($consignee_id>0)
		{
			$consignee_info = M('user_consignee')->where(array('id'=>$consignee_id,'user_id'=>intval($this->uid)))->find();
			if(!$consignee_info&&$deal_item['is_delivery']==1)
			{
                $this->error('请选择配送方式');
			}
			$order_info['consignee'] = $consignee_info['consignee'];
			$order_info['zip'] = $consignee_info['zip'];
			$order_info['address'] = $consignee_info['address'];
			$order_info['province'] = $consignee_info['province'];
			$order_info['city'] = $consignee_info['city'];
			$order_info['mobile'] = $consignee_info['mobile'];
		}
		$order_info['is_success'] = $deal_info['is_success'];
        //锁定购买人数
        $support_count = $deal_item['support_count'] + 1;
        M('deal_item')->where(array('id'=>$deal_item['id']))->data(array('support_count'=>$support_count))->save();

		$order_id = M('deal_order')->data($order_info)->add();
        M('deal_item')->commit();

		if($order_id>0)
		{
			if($order_info['credit_pay']>0)
			{
				
				require_once SITE_PATH . "system/libs/user.php";
				modify_account(array("money"=>"-".$order_info['credit_pay']),intval($this->uid),"支持".$deal_info['name']."项目支付");				
			}
			$result = pay_order($order_id);
			if($result['status']==0)
			{
				$money = $result['money'];
				$payment_notice['create_time'] = TIME_NOW;
				$payment_notice['user_id'] = intval($this->uid);
				$payment_notice['payment_id'] = $payment_id;
				$payment_notice['money'] = $money;
				$payment_notice['bank_id'] = strim($_REQUEST['bank_id']);
				$payment_notice['order_id'] = $order_id;
				$payment_notice['memo'] = $memo;
				$payment_notice['deal_id'] = $deal_info['id'];
				$payment_notice['deal_item_id'] = $deal_item['id'];
				$payment_notice['deal_name'] = $deal_info['name'];
				
				do{
					$payment_notice['notice_sn'] = to_date(TIME_NOW,"Ymdhis").rand(10000,99999);
					$notice_id = M('payment_notice')->data($payment_notice)->add();
				}while($notice_id==0);
                $this->redirect('/cart/jump/',array("id"=>$notice_id),1,'跳转支付');
			}
			elseif($result['status']==1||$result['status']==2)
			{
				$this->redirect('/user.html');
			}
			else
			{
                $this->redirect('/user.html');
			}
		}
		else
		{
			$this->error('下单失败');
		}		
		
	}

    public function repay(){
        $id = intval($_REQUEST['id']);
        $data = M('payment_notice')->where(array('order_id'=>$id,'user_id'=>$this->uid))->order('id desc')->limit(1)->find();
        if($data){
            if($data['is_paid'] == '1'){
                $this->error('订单已经支付过');
            }
            $deal_item = M('deal_item')->where(array('id'=>$data['deal_item_id']))->find();
            if(!$deal_item)
            {
                M('deal_order')->where(array('id'=>$data['order_id'],'order_status'=>'0'))->data(array('order_status'=>'1'))->save();
                $this->error("购买的项目不存在");
            }
            $deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
            if(!$deal_info)
            {
                M('deal_order')->where(array('id'=>$data['order_id'],'order_status'=>'0'))->data(array('order_status'=>'1'))->save();
                $this->error("购买的项目不存在");
            }
            elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
            {
                M('deal_order')->where(array('id'=>$data['order_id'],'order_status'=>'0'))->data(array('order_status'=>'1'))->save();
                $this->redirect('/deal/show/',array("id"=>$deal_item['deal_id']),1,'购买的项目已过期');
            }
            $notice_sn = to_date(TIME_NOW,"Ymdhis").rand(10000,99999);
            M('payment_notice')->data(array('notice_sn'=>$notice_sn))->where(array('id'=>$data['id']))->save();

            M('deal_order')->where(array('id'=>$data['order_id']))->data(array('repay_time'=>time()))->save();
            $this->redirect('/Cart/jump/',array('id'=>$data['id']),1,'跳转支付中...');
        }else{
            $this->error('找不到订单信息');
        }
    }
	
	public function jump()
	{
        $this->require_login();
		
		$notice_id = intval($_REQUEST['id']);
		$notice_info = M('payment_notice')->where(array('id'=>$notice_id,'is_paid'=>'0','user_id'=>intval($this->uid)))->find();
		if(!$notice_info)
		{
            M('deal_order')->where(array('id'=>$notice_info['order_id'],'order_status'=>'0'))->data(array('order_status'=>'1'))->save();
			$this->redirect('/',array(),2,'订单不存在');
		}

        $deal_item = M('deal_item')->where(array('id'=>$notice_info['deal_item_id']))->find();
        if(!$deal_item)
        {
            M('deal_order')->where(array('id'=>$notice_info['order_id']))->data(array('order_status'=>'1'))->save();
            $this->error("购买的项目不存在");
        }
        $deal_info = M('deal')->where(array('is_delete'=>'0','is_effect'=>'1','id'=>$deal_item['deal_id']))->find();
        if(!$deal_info)
        {
            M('deal_order')->where(array('id'=>$notice_info['order_id']))->data(array('order_status'=>'1'))->save();
            $this->error("购买的项目不存在");
        }
        elseif($deal_info['begin_time']>TIME_NOW||($deal_info['end_time']<TIME_NOW&&$deal_info['end_time']!=0))
        {
            M('deal_order')->where(array('id'=>$notice_info['order_id'],'order_status'=>'0'))->data(array('order_status'=>'1'))->save();
            $this->redirect('/deal/show/',array("id"=>$deal_item['deal_id']),1,'购买的项目已过期');
        }


        $payment_info = M('payment')->where(array('id'=>$notice_info['payment_id']))->find();
		$class_name = $payment_info['class_name']."_payment";
		require_once SITE_PATH."system/payment/".$class_name.".php";
		$o = new $class_name;
		
		header("Content-Type:text/html; charset=utf-8");
		echo $o->get_payment_code($notice_id);
	}
	
}
?>